package com.shanghaionstar.user.presenter;

import android.content.Context;
import android.widget.EditText;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.entity.SubscriberDTO;
import com.shanghaionstar.user.iview.IPayCardOtherActiveView;
import com.shanghaionstar.user.service.PayCardOtherActiveService;

import rx.functions.Action1;

/**
 * Created by Mengzhou Gu on 2016/8/3.
 */
public class PayCardOthetActivePresenter extends BasePresenter<SubscriberDTO> {
    private Context                   context;
    private IPayCardOtherActiveView   payCardOtherActiveView;
    private EditText                  et_car_recognition_code;
    private PayCardOtherActiveService service;

    public PayCardOthetActivePresenter(IMVPScheduler mvpScheduler, Context context, EditText et_car_recognition_code) {
        super(mvpScheduler);
        this.context = context;
        this.et_car_recognition_code = et_car_recognition_code;
    }

    @Override
    public void setView(IMVPView<SubscriberDTO> view) {
        super.setView(view);
        service = new PayCardOtherActiveService(context, et_car_recognition_code);
        payCardOtherActiveView = (IPayCardOtherActiveView) view;
        setServiceProvider(service);
    }

    public void getData() {
        payCardOtherActiveView.setLoading(true);
        subscription= service.getObservable().subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<SubscriberDTO>() {
                    @Override
                    public void call(SubscriberDTO subscriberDTO) {
                        if(!CommonUtil.isNull(subscriberDTO)){
                            payCardOtherActiveView.setLoading(false);
                            payCardOtherActiveView.showSuccess(subscriberDTO);
                        }else {
                            payCardOtherActiveView.setLoading(false);
                            payCardOtherActiveView.showFailure();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        payCardOtherActiveView.setLoading(false);
                        payCardOtherActiveView.showError(throwable);
                    }
                });
    }
}
